package com.weiyuwqj.server.service.rebate.impl;

import com.github.pagehelper.PageHelper;
//import com.weiyuwqj.api.hengfeng.model.base.HfResultBean;
//import com.weiyuwqj.api.hengfeng.model.platform.HfPlatformAccountConverse;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.BusPlatformAccountType;
import com.weiyuwqj.server.mapper.BasUserMapper;
import com.weiyuwqj.server.mapper.BusPlatformAccountTypeMapper;
//import com.weiyuwqj.server.pay.BatchTransferApi;
import com.weiyuwqj.server.service.account.module.DealModule;
import com.weiyuwqj.server.service.account.module.action.sys.TransferServerDealAction;
import com.weiyuwqj.server.service.base.BaseServiceImpl;
import com.weiyuwqj.api.constant.Global;
import com.weiyuwqj.api.service.BusBonusRebateService;
import com.weiyuwqj.api.common.PageList;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.exception.HfException;
import com.weiyuwqj.server.mapper.BusBonusRebateMapper;
import com.weiyuwqj.api.model.BusBonusRebate;
import com.weiyuwqj.api.model.vo.BusBonusRebateVo;
//import com.weiyuwqj.api.hengfeng.param.vo.HfBatchtransferVo;
import com.weiyuwqj.api.util.RandomCodesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2016/11/29.
 */
@Service("bonusRebateService")
public class BusBonusRebateServiceImpl extends BaseServiceImpl<BusBonusRebate> implements BusBonusRebateService {
    @Autowired
    private BusBonusRebateMapper bonusRebateMapper;
    @Autowired
    private DealModule dealModule;
    @Autowired
    private BasUserMapper userMapper;
    @Autowired
    private BusPlatformAccountTypeMapper platformAccountTypeMapper;
//    @Autowired
//    private BatchTransferApi batchTransferApi;

    @Override
    public Map updateStatus(String orderNo, Integer oldStatus, Integer newStatus) {
        BusBonusRebate bonusRebate = bonusRebateMapper.getByOrderNo(orderNo);
        if (bonusRebate != null) {
            bonusRebateMapper.updateStatusByOrderNo(orderNo, oldStatus, newStatus);
        }
        return Message.OK;
    }

    @Override
    public PageList getList(Paginator p, Map map) {
        PageHelper.startPage(p.getPageNum(), p.getPageSize());
        List<BusBonusRebateVo> list = bonusRebateMapper.getList(map);
        return new PageList(list);
    }

    @Override
    public List<BusBonusRebateVo> getList(Map map) {
        return bonusRebateMapper.getList(map);
    }


//    @Override
//    public Map transferPlatform2Person(Integer toId, BigDecimal amount, String password, Integer causeType) {
//        if (!password.equals(Global.getServiceRebatePassword())) {
//            throw new CrowdException("user.login.password.password_not_correct");
//        }
//
//        BusPlatformAccountType toAccountType = platformAccountTypeMapper.getByType(1);
//        BasUser fromUser = userMapper.selectByPrimaryKey(Integer.parseInt("1"));
//        String orderNo = "TZ" + RandomCodesUtils.getOrderNo();
//        BusBonusRebate bonusRebate = new BusBonusRebate();
//        bonusRebate.setPayUserId(toAccountType.getUserId());
//        bonusRebate.setReceiveUserId(toId);
//        bonusRebate.setOperType("check_balance");
//        bonusRebate.setAmount(amount);
//        bonusRebate.setOrderNo(orderNo);
//        bonusRebate.setRemark("转账金额:" + amount);
//        bonusRebate.setStatus(1);
//        bonusRebate.setAddtime(new Date());
//        bonusRebateMapper.insert(bonusRebate);
//
//        //出款方资金
//        TransferServerDealAction fromDealAction = new TransferServerDealAction();
//        fromDealAction.deal(bonusRebate.getPayUserId(), bonusRebate.getAmount().negate(),
//                "转账金额" + bonusRebate.getAmount() + "元" + ",收款方ID:" + bonusRebate.getReceiveUserId());
//        dealModule.dealAction(fromDealAction);
//
//        //收款方资金
//        TransferServerDealAction toDealAction = new TransferServerDealAction();
//        toDealAction.deal(bonusRebate.getReceiveUserId(), bonusRebate.getAmount(),
//                "收到转账金额" + bonusRebate.getAmount() + "元" + ",出款方ID:" + bonusRebate.getPayUserId());
//        dealModule.dealAction(toDealAction);
//
//        BasUser user = userMapper.selectByPrimaryKey(toId);
//        HfBatchtransferVo batchtransferVo = new HfBatchtransferVo();
//        batchtransferVo.setAmount(amount);
//        batchtransferVo.setCauseType(causeType);
//        batchtransferVo.setOrderId(orderNo);
//        batchtransferVo.setPayUserId(fromUser.getPlatformId());
//        batchtransferVo.setReceiveUserId(user.getPlatformId());
//	    HfResultBean resultBean = batchTransferApi.platform2Person(batchtransferVo);
//	    //if (!resultBean.isSuccess()) { // 当且仅当返回码为10000时表示请求成功
//	    //   throw new HfException("HfException", resultBean.getMessageText());
//	    //}
//	    return Message.OK;
//    }

//    @Override
//    public Map platformAccountTransfer(Integer fromType, Integer toType, BigDecimal amount, String password) {
//        if (!password.equals(Global.getServiceRebatePassword())) {
//            throw new CrowdException("user.login.password.password_not_correct");
//        }
//
//        BusPlatformAccountType fromAccountType = platformAccountTypeMapper.getByType(fromType);
//        BusPlatformAccountType toAccountType = platformAccountTypeMapper.getByType(toType);
//        String orderNo = "TZ" + RandomCodesUtils.getOrderNo();
//        BusBonusRebate bonusRebate = new BusBonusRebate();
//        bonusRebate.setPayUserId(fromAccountType.getUserId());
//        bonusRebate.setReceiveUserId(toAccountType.getUserId());
//        bonusRebate.setOperType("check_balance");
//        bonusRebate.setAmount(amount);
//        bonusRebate.setOrderNo(orderNo);
//        bonusRebate.setRemark("转账金额:" + amount);
//        bonusRebate.setStatus(1);
//        bonusRebate.setAddtime(new Date());
//        bonusRebateMapper.insert(bonusRebate);
//
//        //出款方资金
//        TransferServerDealAction fromDealAction = new TransferServerDealAction();
//        fromDealAction.deal(bonusRebate.getPayUserId(), bonusRebate.getAmount().negate(),
//                "转账金额" + bonusRebate.getAmount() + "元" + ",收款方ID:" + bonusRebate.getReceiveUserId());
//        int count = dealModule.dealAction(fromDealAction);
//        if (count < 1) {
//            throw new CrowdException("update_error");
//        }
//
//        //收款方资金
//        TransferServerDealAction toDealAction = new TransferServerDealAction();
//        toDealAction.deal(bonusRebate.getReceiveUserId(), bonusRebate.getAmount(),
//                "收到转账金额" + bonusRebate.getAmount() + "元" + ",出款方ID:" + bonusRebate.getPayUserId());
//        count = dealModule.dealAction(toDealAction);
//        if (count < 1) {
//            throw new CrowdException("update_error");
//        }
//
//        HfPlatformAccountConverse accountConverse = new HfPlatformAccountConverse();
//        accountConverse.setAmt(String.valueOf(amount));
//        accountConverse.setDest_account(String.valueOf(toType));
//        accountConverse.setRemark("转账金额:" + amount + "元");
//        accountConverse.setSource_account(String.valueOf(fromType));
//        accountConverse.setOrder_no(orderNo);
//	    HfResultBean resultBean = batchTransferApi.doPlatformAccountConverse(accountConverse);
//	    //if (!resultBean.isSuccess()) { // 当且仅当返回码为10000时表示请求成功
//	    //    throw new HfException("HfException", resultBean.getMessageText());
//	    //}
//	    return Message.OK;
//    }

}
